<template>
    <el-aside 
        class="aside-container"
        @mouseenter.stop="showCollapseIcon = true" 
        @mouseleave.stop="showCollapseIcon = false"
    >
        <el-menu
            background-color="#eeeeee"
            default-active="2"
            class="el-menu-vertical-demo"
            :collapse="isCollapse"
            @open="handleOpen"
            @close="handleClose"
        >
            <!-- 菜单顶部控制区域 -->
            <div class="menu-header">
                <span 
                v-show="!isCollapse && !isCollapsing" 
                class="menu-title"
                >
                导航菜单
                </span>
                <el-icon 
                class="collapse-icon" 
                :class="{ 'show': showCollapseIcon || isCollapse }" 
                @click="startToggleCollapse"
                >
                <component :is="isCollapse ? 'ArrowRight' : 'ArrowLeft'" />
                </el-icon>
            </div>
            
            <!-- 菜单项 -->
            <el-menu-item 
                v-for="item in noChildren"
                :index="item.path"
                :key="item.path"
                @click="goRoute(item)"
            >
                <component class="menu-icon" :is="item.icon"></component>
                <span>{{ item.label }}</span>
            </el-menu-item>

            <!-- 子菜单 -->
            <el-sub-menu
                v-for="item in hasChildren"
                :index="item.path"
                :key="item.path"
            >
                <template #title>
                    <component class="menu-icon" :is="item.icon"></component>
                    <span>{{ item.label }}</span>
                </template>
                <el-menu-item-group>
                    <el-menu-item 
                        v-for="(subItem) in item.children"
                        :index="subItem.path"
                        :key="subItem.path"
                        @click="goRoute(subItem)"
                    >
                        <component class="menu-icon" :is="subItem.icon"></component>
                        <span>{{ subItem.label }}</span>
                    </el-menu-item>
                </el-menu-item-group>
            </el-sub-menu>
        </el-menu>
    </el-aside>
</template>

<script lang="ts" setup>
import { ref , computed } from 'vue'
import {
    ArrowLeft,
    ArrowRight
} from '@element-plus/icons-vue'
import { useRouter } from 'vue-router';

const isCollapse = ref(false)
const isCollapsing = ref(false)
const showCollapseIcon = ref(false)
const router = useRouter();

const goRoute = (item) => {
  router.push(item.path);
};

// 添加emit事件
const emit = defineEmits(['toggle-collapse']);

const startToggleCollapse = () => {
  isCollapsing.value = true;
  showCollapseIcon.value = false;
  
  setTimeout(() => {
    isCollapse.value = !isCollapse.value;
    isCollapsing.value = false;
    emit('toggle-collapse', isCollapse.value); // 触发事件
  }, 0);
}

const handleOpen = (key: string, keyPath: string[]) => {
    console.log(key, keyPath)
}
const handleClose = (key: string, keyPath: string[]) => {
    console.log(key, keyPath)
}


const list = ref([
    {
        path: '/home',
        name: 'home',
        label: '首页',
        icon: 'house',
        url: 'Home'
    },
    {
        path: '/upload',
        name: 'upload',
        label: '上传知识',
        icon: 'promotion',
        url: 'Upload'
    },
    {
        path: '/generate',
        name: 'generate',
        label: '生成图像',
        icon: 'picture',
        url: 'Generate'
    },
    {
        path: '/other',
        label: '其他',
        icon: 'location',
        children: [
            {
            path: '/page1',
            name: 'page1',
            label: '页面1',
            icon: 'setting',
            url: 'Page1'
            },
            {
            path: '/page2',
            name: 'page2',
            label: '页面2',
            icon: 'setting',
            url: 'Page2'
            },
        ]
    },
])
const noChildren = computed(()=>list.value.filter(item=>!item.children))
const hasChildren = computed(()=>list.value.filter(item=>item.children))

</script>

<style scoped>
.aside-container ::-webkit-scrollbar {
  display: none;
}

/* 确保菜单正确布局 */
.el-menu-vertical-demo {
  height: 100vh; 
  overflow-y: auto;
  overflow-x: hidden;
}
/* 容器样式调整 */
.aside-container {
  height: 100vh;
  background-color: var(--morandi-sidebar);
  margin: 0; /* 移除外边距 */
  padding: 0; /* 移除内边距 */
  display: flex; /* 启用flex布局 */
  -ms-overflow-style: none;
  scrollbar-width: none;
  overflow: hidden;
  box-shadow: none;
  position: absolute; /* 改为绝对定位 */
  left: 0;
  top: 0;
  bottom: 0;
}

.el-menu-vertical-demo {
  height: 100vh; /* 减去header高度 */
  overflow-y: auto;
  overflow-x: hidden;
  border-right: none;
  margin: 0; /* 确保菜单无外边距 */
  padding: 0; /* 确保菜单无内边距 */
}
/* 图标统一大小设置 */
.menu-icon {
    width: 1em;
    height: 1em;
    font-size: 18px; 
    margin-right: 8px;
    flex-shrink: 0;
}

/* 折叠按钮图标大小 */
.collapse-icon {
    font-size: 16px;
    width: 1em;
    height: 1em;
}


.menu-title {
    font-size: 16px;
    font-weight: 500;
    color: var(--el-text-color-primary);
}

.collapse-icon {
    cursor: pointer;
    color: var(--el-text-color-secondary);
    transition: all 0.3s ease;
    opacity: 0;
}

.collapse-icon.show {
    opacity: 1;
}

.collapse-icon:hover {
    color: var(--el-color-primary);
}

.el-menu-vertical-demo:not(.el-menu--collapse) {
    width: 220px;
}

.fade-leave-active {
    transition: opacity 0.1s ease;
    position: absolute; /* 防止挤压效果 */
    left: 20px; /* 与menu-header的padding-left一致 */
}
.fade-leave-to {
    opacity: 0;
}

/* 调整menu-header样式 */
.menu-header {
    position: relative; 
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    height: 56px;
    box-sizing: border-box;
    border-bottom: 1px solid var(--el-border-color-light);
    overflow: hidden; /* 防止内容溢出 */
}

</style>
<!-- <style scoped>
/* 全局滚动条隐藏 */
.aside-container ::-webkit-scrollbar {
  display: none;
}
.aside-container {
  -ms-overflow-style: none;
  scrollbar-width: none;
}

/* 容器基础样式 */
.aside-container {
  height: 100vh;
  background-color: #eeeeee;
  margin: 0;
  padding: 0;
  display: flex;
  transition: width 0.3s ease;
  overflow: hidden;
}

/* 菜单主体 */
.el-menu-vertical-demo {
  height: 100vh;
  overflow-y: auto;
  overflow-x: hidden;
  border-right: none;
  transition: width 0.3s ease, min-width 0.3s ease;
}

/* 展开状态 */
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 220px;
  min-width: 220px;
}

/* 收起状态 */
.el-menu--collapse {
  width: 64px;
  min-width: 64px;
}

/* 文字过渡效果 */
.el-menu-vertical-demo .el-menu-item span,
.el-menu-vertical-demo .el-sub-menu__title span {
  transition: opacity 0.2s ease, width 0.3s ease;
  display: inline-block;
}
.el-menu--collapse .el-menu-item span,
.el-menu--collapse .el-sub-menu__title span {
  opacity: 0;
  width: 0;
}

/* 图标设置 */
.menu-icon {
  width: 1em;
  height: 1em;
  font-size: 18px;
  margin-right: 8px;
  flex-shrink: 0;
  transition: margin 0.3s ease;
}
.el-menu--collapse .menu-icon {
  margin-right: 0;
}

/* 折叠按钮 */
.collapse-icon {
  font-size: 16px;
  width: 1em;
  height: 1em;
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  cursor: pointer;
  color: #666;
  transition: all 0.3s ease;
  opacity: 0;
}
.collapse-icon.show {
  opacity: 1;
}
.collapse-icon:hover {
  color: #409eff;
}

/* 菜单头部 */
.menu-header {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  height: 56px;
  box-sizing: border-box;
  border-bottom: 1px solid #e6e6e6;
  transition: padding 0.3s ease;
}
.el-menu--collapse .menu-header {
  padding: 0 12px;
}
.menu-title {
  font-size: 16px;
  font-weight: 500;
  color: #333;
  transition: opacity 0.3s ease;
}
</style> -->